'''
购物车Cart
购物车属性id
方法添加购物车，删除某个商品，清空购物车，购物车商品统计结算
商品Product
商品属性价格price,id,name,detail
'''
class Cart:

    def __init__(self,id):
        self.id = id
        self.cartList = []
    def addCart(self,product):
        for item in self.cartList:
            if item["product"].name == product.name:
                item["product"].count += 1
                return {"status": 200,"msg":"商品数量添加成功"}
        self.cartList.append({"product":product,"count":1})
        return {"status": 200, "msg": "商品添加成功"}
    def removeCart(self,product):
        for item in self.cartList:
            if item["product"].name == product.name:
                self.cartList.remove(item)
                return {"status": 200, "msg": "商品删除成功"}
            else:
                return {"status": 400, "msg": "商品不存在"}
    def clearCart(self):
        self.cartList.clear()
    def payOrderSave(self):
        totalPrice = 0
        for item in self.cartList:
            totalPrice += item["product"].price * item["count"]
        return {"status": 200,"totalPrice":totalPrice, "msg": "结算成功"}
class Product():
    def __init__(self,id,name,price,detail):
        self.price = price
        self.id = id
        self.name = name
        self.detail = detail
if __name__ == '__main__':
    product = Product(1,"水杯",12,"喝咖啡好用")
    product2 = Product(2, "牛奶", 5, "喝牛奶")
    product3 = Product(3, "面包", 3, "早餐吃")

    cart = Cart(1)
    print(cart.cartList)
    cart.addCart(product)
    cart.addCart(product2)
    cart.addCart(product3)
    print(cart.cartList)
    print(cart.payOrderSave())